# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import simplejson as json
import gzip
import datetime
from CrawlerLib.http import Http

FILE_DATA = 'naruto.data'

def loadData(fileName):
    f = gzip.open(fileName, 'rb')
    content = f.read()
    f.close()
    data = json.loads(content)
    return data;

def saveData(fileName, data):
    content = json.dumps(data)
    f = gzip.open(fileName, 'wb')
    f.write(content)
    f.close()

def pushFirst(item):
    data = loadData(FILE_DATA)
    n = len(data)
    data.append(None)
    for i in range(n, 0, -1):
        data[i] = data[i-1]
    data[0] = item;
    saveData(FILE_DATA, data)

def pushLast(item):
    data = loadData(FILE_DATA)
    data.append(item)
    saveData(FILE_DATA, data)


def reverse():
    data = loadData(FILE_DATA)
    data.reverse()
    for i in range(0, len(data)):
        item = data[i]
        if item['chapter'] == 'NARUTO chap 582': data.remove(item)
    saveData(FILE_DATA, data)

def getImagesOfChapter(url, chapterName):
    
    tree = Http.getXMLTree(url)
    xpathNode = "//div[@class='entry']//img"
    for item in tree.xpath(xpathNode):
        print item.get('src')
    
def update(chapterName):
    
    images = '''http://4.bp.blogspot.com/-8WFI9ODkCls/T4WEddz14iI/AAAAAAAA-qo/nerHNsQKRRQ/s1200/01.jpg
http://4.bp.blogspot.com/-FmyDQ1Sa4Jo/T4WEc12QpGI/AAAAAAAA-qg/dSTfSn2Zujs/s1200/02.jpg
http://4.bp.blogspot.com/-RDOzeARfaK0/T4WEdcfZe6I/AAAAAAAA-qk/wSsPeF--WJk/s1200/03.jpg
http://4.bp.blogspot.com/-DU13kOvfqvM/T4WEfXfx_sI/AAAAAAAA-q4/9HA68ktww8Y/s1200/04.jpg
http://4.bp.blogspot.com/-4oXhQS4aMQQ/T4WEgmWm8BI/AAAAAAAA-rA/UeBTOBVYgYI/s1200/05.jpg
http://4.bp.blogspot.com/-uiRCyMpTzVs/T4WEiQHfCoI/AAAAAAAA-rI/7GDkqeUAHSk/s1200/06.jpg
http://3.bp.blogspot.com/-jVfELEBNZog/T4WEi8na5KI/AAAAAAAA-rM/qIPF50v2vyw/s1200/07.jpg
http://3.bp.blogspot.com/-fg-vkKa3QTk/T4WEkbi0kjI/AAAAAAAA-rY/yZS8Ayhmd04/s1200/08.jpg
http://3.bp.blogspot.com/-NwslhQm_GEM/T4WElJwx7PI/AAAAAAAA-rg/Rfw504IfNxU/s1200/09.jpg
http://3.bp.blogspot.com/-MSYinzP8RQI/T4WEm4rWBmI/AAAAAAAA-ro/niTMUoXCJLA/s1200/10.jpg
http://4.bp.blogspot.com/-JRZ85AEc1Xw/T4WEnmhcA9I/AAAAAAAA-rs/xv99-SICGH0/s1200/11.jpg
http://4.bp.blogspot.com/-_1IsIqLI2UE/T4WEpK4CktI/AAAAAAAA-r4/fhvOq1pWKlg/s1200/12.jpg
http://4.bp.blogspot.com/-PpSlnf4fyE8/T4WEpzo9EkI/AAAAAAAA-r8/uC-zQWzgzsk/s1200/13.jpg
http://4.bp.blogspot.com/-owRSoLS7WIg/T4WErNlsReI/AAAAAAAA-sI/nnQ2tQemCoE/s1200/14.jpg
http://4.bp.blogspot.com/-C_lwA35-Wxg/T4WEsAqOPvI/AAAAAAAA-sM/WcFnWGNGTdw/s1200/15.jpg
http://4.bp.blogspot.com/-7tGuxX-dfxw/T4WEs8mFF4I/AAAAAAAA-sU/0E3YRWXT9xk/s1200/16.jpg
http://4.bp.blogspot.com/-zsPDzfTWyJE/T4WEub4SR1I/AAAAAAAA-sk/mNPU24yYH-w/s1200/17.jpg
http://4.bp.blogspot.com/-PxQlFccC8GY/T4WEt8zfoBI/AAAAAAAA-sc/ZfZcn5wnvAk/s1200/18.jpg
http://4.bp.blogspot.com/-A1Pbmo6Yg4E/T4WEukOhazI/AAAAAAAA-so/p0vwI0gfXFs/s1200/19.jpg'''
    
    images = images.strip().split("\n")
    item = {'chapter': chapterName, 'images': images}
    pushFirst(item)
        
if __name__ == '__main__':
    
#    getImagesOfChapter('http://doctruyen.vechai.info/naruto-chap-582/', 'NARUTO chap 582')
    update('NARUTO chap 582')
#    reverse()
    print('>>Finished at {0}'.format(datetime.datetime.now()))
    os._exit(1)

    